package com.basketball.gym.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.basketball.gym.annotation.ApiJsonObject;
import com.basketball.gym.annotation.ApiJsonProperty;
import com.basketball.gym.pojo.Admin;
import com.basketball.gym.pojo.ResponseData;
import com.basketball.gym.pojo.state.ResponseState;
import com.basketball.gym.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;

/**
 *
 * @author 赵肖云
 * @since 2021-03-23
 */
@Api(tags = "管理员管理")
@RestController
@RequestMapping("/gym/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @ApiOperation("登录")
    @PostMapping("/login")
    public ResponseData<Admin> login(@Validated(value = Admin.login.class) @RequestBody Admin activeAdmin) {
        return adminService.login(activeAdmin);
    }

    @ApiOperation("添加管理员")
    @PreAuthorize("hasRole('superAdmin')")
    @PostMapping("/add")
    public ResponseData<Admin> addAdmin(@Validated(value = Admin.addAdmin.class) @RequestBody Admin admin) {
        try {
            return adminService.addAdmin(admin);
        } catch (DuplicateKeyException e) {
            return new ResponseData<>(ResponseState.USER_IS_EXIST.getMessage(), ResponseState.USER_IS_EXIST.getValue());
        }
    }

    @ApiOperation("更新Token")
    @PostMapping("/refresh")
    public ResponseData<Object> refreshToken(@ApiJsonObject(name = "AdminRefreshToken", value = {
            @ApiJsonProperty(key = "refreshToken")
    }) @RequestBody Map<String, Object> param) {
        String refreshToken = (String) param.get("refreshToken");
        return adminService.refreshToken(refreshToken);
    }

    @ApiOperation("退出登录")
    @PreAuthorize("hasAnyRole('admin','superAdmin')")
    @DeleteMapping("/logout")
    public ResponseData<Object> logout(@ApiIgnore @ModelAttribute("activeAdmin") Admin admin) {
        return adminService.logout(admin);
    }

    @ApiOperation("查询个人信息")
    @PreAuthorize("hasAnyRole('admin','superAdmin')")
    @GetMapping
    public ResponseData<Admin> getAdmin(@ApiIgnore @ModelAttribute("activeAdmin") Admin admin) {
        return adminService.getAdmin(admin);
    }

    @ApiOperation("修改个人信息")
    @PreAuthorize("hasAnyRole('admin','superAdmin')")
    @PutMapping
    public ResponseData<Admin> updateAdmin(@RequestBody Admin admin, @ApiIgnore @ModelAttribute("activeAdmin") Admin activeAdmin) {
        return adminService.updateAdmin(admin, activeAdmin);
    }

    @ApiOperation("查询管理员列表")
    @PreAuthorize("hasAnyRole('superAdmin')")
    @GetMapping("/all")
    public ResponseData<IPage<Admin>> getAdmins(Admin admin) {
        return adminService.getAdmins(admin);
    }

    @ApiOperation("超管修改管理员的信息")
    @PreAuthorize("hasRole('superAdmin')")
    @PutMapping("/commonAdmin")
    public ResponseData<Object> updateCommonAdmin(@Validated(Admin.update.class) @RequestBody Admin admin) {
        return adminService.updateCommonAdmin(admin);
    }

    @ApiOperation("删除管理员")
    @PreAuthorize("hasRole('superAdmin')")
    @PostMapping("/delete")
    public ResponseData<Object> deleteAdmin(@RequestBody Map<String,Object> param){
        Integer id= (Integer) param.get("id");
        return adminService.deleteAdmin(id);
    }
}

